package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.alibaba.fastjson.JSON;
import org.jeecg.modules.monitoring.j_monitoring.entity.MHydrogen;
import org.jeecg.modules.monitoring.j_monitoring.entity.MHydrogen;
import org.jeecg.modules.monitoring.j_monitoring.entity.RingDevices;
import org.jeecg.modules.monitoring.j_monitoring.entity.RingMonitor;
import org.jeecg.modules.monitoring.j_monitoring.entity.donghuan.realDataRequest;
import org.jeecg.modules.monitoring.j_monitoring.entity.donghuan.realPoints;
import org.jeecg.modules.monitoring.j_monitoring.facilities.FacilitiesToken;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MHydrogenMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMHydrogenService;
import org.jeecg.modules.monitoring.j_monitoring.service.IRingDevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 氢气
 * @Author: xiaoyq
 * @Date:   2023-07-21
 * @Version: V1.0
 */
@Service
public class MHydrogenServiceImpl extends ServiceImpl<MHydrogenMapper, MHydrogen> implements IMHydrogenService {

    @Autowired
    private IRingDevicesService ringDevicesService;

    @Autowired
    private FacilitiesToken facilitiesToken;

    @Autowired
    private RingMonitorServiceImpl ringMonitorService;

    @Autowired
    private Environment env;

    public void getHttpData(List<MHydrogen> dataList) {

        if (dataList.size() == 0) {
            return;
        }

        if(!"prod".equals(env.getProperty("spring.profiles.active"))){
            return;
        }

        List<String> nameList = dataList.stream().map(e -> e.getPoint()).collect(Collectors.toList());

        //设备list
        List<RingDevices> devicesList = ringDevicesService.getListByNameList(nameList);
        List<String> devicesIdList  = devicesList.stream().map(e->e.getResourceId()).collect(Collectors.toList());
        Map<String,String > devicesMap = devicesList.stream().collect(Collectors.toMap(
                item -> item.getName(),
                item -> item.getResourceId(),
                (o1, o2) -> o2)  //当key值一致时取后者
        );


        //测点list
        List<RingMonitor> monitorList = ringMonitorService.getRingMonitorByIds(devicesIdList);

        String json = facilitiesToken.getOnlineData(devicesIdList);
        realDataRequest request = JSON.parseObject(json, realDataRequest.class);
        //请求返回值list
        List<realPoints> pointsList = request.getData().getPoints();

        for (MHydrogen hydrogen : dataList) {

            String id = devicesMap.get(hydrogen.getPoint());

            for (RingMonitor monitor : monitorList){

                if ("气体浓度值".equals(monitor.getName()) && monitor.getResourceId().contains(id) ){
                    String value = ringMonitorService.getDataValue(monitor.getResourceId(),pointsList);
                    hydrogen.setConcentration(value + monitor.getUnit());
                }


            }
        }


    }


}
